package com.ly.simple;

import com.ly.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    static final String QUEUE_NAME = "simple_queue";//简单队列

    public static void main(String[] args) {
        //从工具类中获取连接
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();//创建频道
            // 声明（创建）队列
            /***
             * 参数1：队列名称
             * 参数2：是否定义持久化队列 *
             * 参数3：是否独占本次连接 *
             * 参数4：是否在不使用的时候自动删除队列 *
             * 参数5：队列其它参数 */
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);

            String message = "hello,world";//要发送的消息
            //发送
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println("消息发送成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (null != channel) {
                    channel.close();
                }
                if (null != connection) {
                    connection.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }


    }
}
